<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<!-- saved from url=(0074)http://docs.oracle.com/javase/tutorial/uiswing/components/filechooser.html -->
<html lang="en-US" xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>How to Use File Choosers (The Java™ Tutorials &gt;        
            Creating a GUI With JFC/Swing &gt; Using Swing Components)
</title>

     <meta name="description" content="This Swing Java Tutorial describes developing graphical user interfaces (GUIs) for applications and applets using Swing components">
     <meta name="keywords" content="java programming, learn java, java sample code, java swing, java ui, java user interface, swing components, swing events, swing layout  ">
        
<style type="text/css">
    .FigureCaption   { 
        margin-left: 1in; 
        margin-right: 1in; 
        font-family: sans-serif; 
        font-size: smaller; 
        text-align: justify;
    }
    #TopBar_bl {
        background: url(../../images/java_bar_bl.gif) 0 100% no-repeat;
        width: 100%;
        height: 60px;
    }
    #TopBar_br {
        background: url(../../images/java_bar_br.gif) 100% 100% no-repeat;
        width: 100%;
        height: 60px;
    }
    #TopBar_tl {
        background: url(../../images/java_bar_tl.gif) 0 0 no-repeat;
        width: 100%;
        height: 60px;
    }
    #TopBar_tr {
        background: url(../../images/java_bar_tr.gif) 100% 0 no-repeat;
        width: 100%;
        height: 60px;
    }
    #TopBar {
        background: #35556B url(../../images/java_bar.gif);
        margin: 10px 10px 0 10px;
        height:60px;
        min-width:700px;
        color: white;
        font-family: sans-serif; 
        font-weight: bold;
    }
    @media print {
        #BreadCrumbs, #Download {
            display: none;
        }
    }
    #TopBar_right {
        line-height: 14px;
        float: right;
        padding-top: 2px;
        padding-right: 30px;
        text-align: center;
    }
    @media print {
        #TopBar_right {
            display: none;
        }
    }
    #TopBar_right a {
        font-size: 12px;
        margin: 3px;
        padding: 0;
    }
    #TopBar a:visited, #TopBar a:link {
        color: white;
        text-decoration: none;
    }
    #TopBar a:hover, #TopBar a:active  {
        background-color: white;
        color: #35556B;
    }
    #BreadCrumbs {
        padding: 4px 5px 0.5em 0;
        font-family: sans-serif; 
        float: right;
    }
    #BreadCrumbs a {
        color: blue;
    }
    #BreadCrumbs a:visited, #BreadCrumbs a:link {
        text-decoration: none;
    }
    #BreadCrumbs a:hover, #BreadCrumbs a:active {
        text-decoration: underline;
    }
    #PageTitle {
        margin: 0 5px 0.5em 0;
        color: #F90000;
    }    
    #PageContent{
        margin: 0 5px 0 20px;
    }
    .LeftBar_shown {
        width: 13em;
        float: left;
        margin-left: 10px;
        margin-top: 4px;
        margin-bottom: 2em;
        margin-right: 10px;
    }
    @media print {
        .LeftBar_shown {
            display: none;
        }
    }
    .LeftBar_hidden {
        display: none;
    }
    #Footer {
        padding-top: 10px;
        padding-left: 10px;
        margin-right: 10px;
    }
    
    .footertext {
        font-size: 10px;
        font-family: sans-serif; 
        margin-top: 1px;
    }
    
    #Footer2 {
        padding-top: 10px;
        padding-left: 10px;
        margin-right: 10px;
    }
    .NavBit  {
        padding: 4px 5px 0.5em 0;
        font-family: sans-serif; 
    }
    @media print {
        .NavBit {
            display: none;
        }
    }
    #TagNotes {
        text-align: right;        
    }
    
    @media print {
        #TagNotes a:visited, #TagNotes a:link {
            color: #35556B;
            text-decoration: none;
        }
    }
    #Contents a, .NavBit a, #TagNotes a {
        color: blue
    }
    #TagNotes a:visited, #TagNotes a:link,
    #Contents a:visited, #Contents a:link,
    .NavBit a:visited, .NavBit a:link {
        text-decoration: none;
    }
    #TagNotes a:hover, #TagNotes a:active,   
    #Contents a:hover, #Contents a:active,   
    .NavBit a:hover, .NavBit a:active {  
        text-decoration: underline;
    }
    #Contents {
        float: left;
        font-family: sans-serif; 
    }
    @media print {
        #Contents {
            display: none;
        }
    }
    @media screen {
        div.PrintHeaders {
            display: none;
        }
    }
    .linkLESSON, .nolinkLESSON {
        margin-left: 0.5em;
        text-indent: -0.5em
    }
    .linkAHEAD, .nolinkAHEAD, .linkQUESTIONS, .nolinkQUESTIONS   {
        margin-left: 1.5em; 
        text-indent: -0.5em
    }
    .linkBHEAD, .nolinkBHEAD   {
        margin-left: 2.5em;
        text-indent: -0.5em
    }
    .linkCHEAD, .nolinkCHEAD   {
        margin-left: 3.5em;
        text-indent: -0.5em
    }
    .nolinkLESSON, .nolinkAHEAD, .nolinkBHEAD, .nolinkCHEAD,
    .nolinkQUESTIONS {
        font-weight: bold;
        color: #F90000;
    }
    .MainFlow_indented {
        margin-right: 10px;
        margin-left: 15em;
        margin-bottom: 2em;

    }
    .MainFlow_wide {
        margin-right: 10px;
        margin-left: 10px;
        margin-bottom: 2em;

    }
    @media print {
        .MainFlow_indented, .MainFlow_wide {
            padding-top: 0;
            margin-top: 10px;
            margin-right: 10px;
            margin-left: 0;
        }
    }
    h1, h2, h3, h4, h5 {
        color: #F90000;
        font-family: sans-serif;
    }

    h1 {
        font-weight: bold;
        font-size: 20px;
    }

    h2 {
        font-weight: bold;
        font-size: 17px;
    }

    h3 {
        font-weight: bold;
        font-size: 14px;
    }

    h4 {
        font-size: 15px;
    }

    h5 {
        font-size: 12px;
    }


    #ToggleLeft {
        display: none;
    }
    
    .note {
        margin: 0 30px 0px 30px;
    }
    
    .codeblock {
        margin: 0 30px 0px 30px;
    }
    
    .tocli {
        list-style-type:none;
    }

</style>
<script type="text/javascript">
/* <![CDATA[ */
    function leftBar() {
        var nameq = 'tutorial_showLeftBar='
        var cookies = document.cookie.split(';');
        for (var i = 0; i < cookies.length; i++) {
            var cookieString = cookies[i];
            while (cookieString.charAt(0) == ' ') {
                cookieString = cookieString.substring(1, cookieString.length);
            }
            if (cookieString.indexOf(nameq) == 0) {
                cookieValue =  cookieString.substring(nameq.length,
                        cookieString.length);
                return cookieValue == 'yes';
            }
        }
        return true;
    }

    function showLeft(b) {
        var contents = document.getElementById("LeftBar");
        var main = document.getElementById("MainFlow");
        var toggle = document.getElementById("ToggleLeft");
        if (b) {
            contents.className = "LeftBar_shown";
            main.className = "MainFlow_indented";
            toggle.innerHTML = "Hide the TOC";
            document.cookie = 'tutorial_showLeftBar=yes; path=/';
        } else {
            contents.className = "LeftBar_hidden";
            main.className = "MainFlow_wide";
            toggle.innerHTML = "Show the TOC";
            document.cookie = 'tutorial_showLeftBar=no; path=/';
        }
    }

    function toggleLeft() {
        showLeft(document.getElementById("LeftBar").className ==
                "LeftBar_hidden");
        document.getElementById("ToggleLeft").blur();
    }

    function load() {
        showLeft(leftBar());
        document.getElementById("ToggleLeft").style.display="inline";
    }

    function showCode(displayCodePage, codePath) {
        var codePathEls = codePath.split("/");
        var currDocPathEls = location.href.split("/");
        //alert ("codePathEls = " + codePathEls + "\n" + "currDocPathEls = " + currDocPathEls);
        currDocPathEls.pop(); // remove file name at the end
        while (codePathEls.length > 0) {
            if (codePathEls[0] == "..") {
                codePathEls.shift();
                currDocPathEls.pop();
            } else {
                break;
            }
        }
        var fullCodePath = currDocPathEls.join("/") + "/" + codePathEls.join("/");
        //alert ("fullCodePath = " + fullCodePath );
        if (codePath.indexOf(".java") != -1 || codePath.indexOf(".jnlp") != -1) {
            window.location.href = displayCodePage + "?code=" + encodeURI(fullCodePath);
        } else {
            window.location.href = fullCodePath;
        }
    }
/* ]]> */    
</script>


    </head>
<body onload="load()">
    <noscript>
        A browser with JavaScript enabled is required for this page to operate properly.
    </noscript>

    <div id="TopBar"> <div id="TopBar_tr"> <div id="TopBar_tl"> <div id="TopBar_br"> <div id="TopBar_bl"> 
                        <div id="TopBar_right"> 
                            <a target="_blank" href="http://www.oracle.com/technetwork/java/javase/downloads/index.html">Download
                                the JDK</a>
                            <br>
                            <a href="http://docs.oracle.com/javase/tutorial/search.html" target="_blank">Search the
                                Tutorials</a>
                            <br>
                            <a href="javascript:toggleLeft()" id="ToggleLeft" style="display: inline; ">Hide the TOC</a>
                        </div>
                    </div> </div> </div> </div> </div>
    <div class="PrintHeaders">
        <b>Trail:</b> Creating a GUI With JFC/Swing
        <br><b>Lesson:</b> Using Swing Components
        <br><b>Section:</b> How to Use Various Components
    </div>

    <div id="LeftBar" class="LeftBar_shown">
        <div id="Contents">
            <div class="linkLESSON"><a href="http://docs.oracle.com/javase/tutorial/uiswing/components/index.html">Using Swing Components</a></div>
<div class="linkAHEAD"><a href="http://docs.oracle.com/javase/tutorial/uiswing/components/toplevel.html">Using Top-Level Containers</a></div>
<div class="linkAHEAD"><a href="http://docs.oracle.com/javase/tutorial/uiswing/components/jcomponent.html">The JComponent Class</a></div>
<div class="linkAHEAD"><a href="http://docs.oracle.com/javase/tutorial/uiswing/components/text.html">Using Text Components</a></div>
<div class="linkBHEAD"><a href="http://docs.oracle.com/javase/tutorial/uiswing/components/generaltext.html">Text Component Features</a></div>
<div class="linkBHEAD"><a href="http://docs.oracle.com/javase/tutorial/uiswing/components/textapi.html">The Text Component API</a></div>
<div class="linkAHEAD"><a href="http://docs.oracle.com/javase/tutorial/uiswing/components/componentlist.html">How to Use Various Components</a></div>
<div class="linkBHEAD"><a href="http://docs.oracle.com/javase/tutorial/uiswing/components/applet.html">How to Make Applets</a></div>
<div class="linkBHEAD"><a href="http://docs.oracle.com/javase/tutorial/uiswing/components/button.html">How to Use Buttons, Check Boxes, and Radio Buttons</a></div>
<div class="linkBHEAD"><a href="http://docs.oracle.com/javase/tutorial/uiswing/components/buttongroup.html">How to Use the ButtonGroup Component</a></div>
<div class="linkBHEAD"><a href="http://docs.oracle.com/javase/tutorial/uiswing/components/colorchooser.html">How to Use Color Choosers</a></div>
<div class="linkBHEAD"><a href="http://docs.oracle.com/javase/tutorial/uiswing/components/combobox.html">How to Use Combo Boxes</a></div>
<div class="linkBHEAD"><a href="http://docs.oracle.com/javase/tutorial/uiswing/components/dialog.html">How to Make Dialogs</a></div>
<div class="linkBHEAD"><a href="http://docs.oracle.com/javase/tutorial/uiswing/components/editorpane.html">How to Use Editor Panes and Text Panes</a></div>
<div class="nolinkBHEAD">How to Use File Choosers</div>
<div class="linkBHEAD"><a href="http://docs.oracle.com/javase/tutorial/uiswing/components/formattedtextfield.html">How to Use Formatted Text Fields</a></div>
<div class="linkBHEAD"><a href="http://docs.oracle.com/javase/tutorial/uiswing/components/frame.html">How to Make Frames (Main Windows)</a></div>
<div class="linkBHEAD"><a href="http://docs.oracle.com/javase/tutorial/uiswing/components/internalframe.html">How to Use Internal Frames</a></div>
<div class="linkBHEAD"><a href="http://docs.oracle.com/javase/tutorial/uiswing/components/label.html">How to Use Labels</a></div>
<div class="linkBHEAD"><a href="http://docs.oracle.com/javase/tutorial/uiswing/components/layeredpane.html">How to Use Layered Panes</a></div>
<div class="linkBHEAD"><a href="http://docs.oracle.com/javase/tutorial/uiswing/components/list.html">How to Use Lists</a></div>
<div class="linkBHEAD"><a href="http://docs.oracle.com/javase/tutorial/uiswing/components/menu.html">How to Use Menus</a></div>
<div class="linkBHEAD"><a href="http://docs.oracle.com/javase/tutorial/uiswing/components/panel.html">How to Use Panels</a></div>
<div class="linkBHEAD"><a href="http://docs.oracle.com/javase/tutorial/uiswing/components/passwordfield.html">How to Use Password Fields</a></div>
<div class="linkBHEAD"><a href="http://docs.oracle.com/javase/tutorial/uiswing/components/progress.html">How to Use Progress Bars</a></div>
<div class="linkBHEAD"><a href="http://docs.oracle.com/javase/tutorial/uiswing/components/rootpane.html">How to Use Root Panes</a></div>
<div class="linkBHEAD"><a href="http://docs.oracle.com/javase/tutorial/uiswing/components/scrollpane.html">How to Use Scroll Panes</a></div>
<div class="linkBHEAD"><a href="http://docs.oracle.com/javase/tutorial/uiswing/components/separator.html">How to Use Separators</a></div>
<div class="linkBHEAD"><a href="http://docs.oracle.com/javase/tutorial/uiswing/components/slider.html">How to Use Sliders</a></div>
<div class="linkBHEAD"><a href="http://docs.oracle.com/javase/tutorial/uiswing/components/spinner.html">How to Use Spinners</a></div>
<div class="linkBHEAD"><a href="http://docs.oracle.com/javase/tutorial/uiswing/components/splitpane.html">How to Use Split Panes</a></div>
<div class="linkBHEAD"><a href="http://docs.oracle.com/javase/tutorial/uiswing/components/tabbedpane.html">How to Use Tabbed Panes</a></div>
<div class="linkBHEAD"><a href="http://docs.oracle.com/javase/tutorial/uiswing/components/table.html">How to Use Tables</a></div>
<div class="linkBHEAD"><a href="http://docs.oracle.com/javase/tutorial/uiswing/components/textarea.html">How to Use Text Areas</a></div>
<div class="linkBHEAD"><a href="http://docs.oracle.com/javase/tutorial/uiswing/components/textfield.html">How to Use Text Fields</a></div>
<div class="linkBHEAD"><a href="http://docs.oracle.com/javase/tutorial/uiswing/components/toolbar.html">How to Use Tool Bars</a></div>
<div class="linkBHEAD"><a href="http://docs.oracle.com/javase/tutorial/uiswing/components/tooltip.html">How to Use Tool Tips</a></div>
<div class="linkBHEAD"><a href="http://docs.oracle.com/javase/tutorial/uiswing/components/tree.html">How to Use Trees</a></div>
<div class="linkAHEAD"><a href="http://docs.oracle.com/javase/tutorial/uiswing/components/html.html">How to Use HTML in Swing Components</a></div>
<div class="linkAHEAD"><a href="http://docs.oracle.com/javase/tutorial/uiswing/components/model.html">How to Use Models</a></div>
<div class="linkAHEAD"><a href="http://docs.oracle.com/javase/tutorial/uiswing/components/icon.html">How to Use Icons</a></div>
<div class="linkAHEAD"><a href="http://docs.oracle.com/javase/tutorial/uiswing/components/border.html">How to Use Borders</a></div>
<div class="linkAHEAD"><a href="http://docs.oracle.com/javase/tutorial/uiswing/components/problems.html">Solving Common Component Problems</a></div>
</div>
    </div>
    <div id="MainFlow" class="MainFlow_indented">
            <span id="BreadCrumbs">
                <a href="http://docs.oracle.com/javase/tutorial/index.html" target="_top">Home Page</a>
                &gt;
                <a href="http://docs.oracle.com/javase/tutorial/uiswing/index.html" target="_top">Creating a GUI With JFC/Swing</a>
                &gt;
                <a href="http://docs.oracle.com/javase/tutorial/uiswing/components/index.html" target="_top">Using Swing Components</a>
            </span>
            <div class="NavBit">
                <a target="_top" href="http://docs.oracle.com/javase/tutorial/uiswing/components/editorpane.html">«&nbsp;Previous</a>&nbsp;•&nbsp;<a target="_top" href="http://docs.oracle.com/javase/tutorial/uiswing/TOC.html">Trail</a>&nbsp;•&nbsp;<a target="_top" href="http://docs.oracle.com/javase/tutorial/uiswing/components/formattedtextfield.html">Next&nbsp;»</a>
            </div>
            <div id="PageTitle"><h1>How to Use File Choosers</h1></div>
            <div id="PageContent">

<p>File choosers provide a GUI for navigating the file system, and then either choosing a file or directory from a list, or entering the name of a file or directory. To display a file chooser, you usually use the <code>JFileChooser</code> API to show a modal <a href="http://docs.oracle.com/javase/tutorial/uiswing/components/dialog.html">dialog</a> containing the file chooser. Another way to present a file chooser is to add an instance of 
<a class="APILink" target="_blank" href="http://docs.oracle.com/javase/7/docs/api/javax/swing/JFileChooser.html"><code>JFileChooser</code></a> to a container.</p>
<div class="note"><hr><strong>Note:</strong>&nbsp;<p>If you intend to distribute your program as an unsigned Java™ Web Start application, then instead of using the <code>JFileChooser</code> API you should use the file services provided by the JNLP API. These services — <code>FileOpenService</code> and <code>FileSaveService</code> — not only provide support for choosing files in a restricted environment, but also take care of actually opening and saving them. An example of using these services is in <a href="http://docs.oracle.com/javase/tutorial/uiswing/examples/components/index.html#JWSFileChooserDemo">JWSFileChooserDemo</a>. Documentation for using the JNLP API can be found in the 
<a class="TutorialLink" target="_top" href="http://docs.oracle.com/javase/tutorial/deployment/webstart/index.html">Java Web Start</a> lesson.</p>
<hr></div>
<p>Click the Launch button to run JWSFileChooserDemo using 
<a class="TutorialLink" target="_top" href="">Java™ Web Start</a> (<a href="http://www.oracle.com/technetwork/java/javase/downloads/index.html">download JDK 6 or later</a>). Alternatively, to compile and run the example yourself, consult the <a href="http://docs.oracle.com/javase/tutorial/uiswing/examples/components/index.html#JWSFileChooserDemo">example index</a>.</p>
<a href="http://docs.oracle.com/javase/tutorialJWS/uiswing/components/ex6/JWSFileChooserDemo.jnlp"><img src="./File Choosers_files/jws-launch-button.png" width="88" height="23" align="bottom" alt="Launches the JWSFileChooserDemo application"></a><br>
<p>When working with the <code>JWSFileChooserDemo</code> example, be careful not to lose files that you need. Whenever you click the save button and select an existing file, this demo brings up the File Exists dialog box with a request to replace the file. Accepting the request overwrites the file.</p>
<p>The rest of this section discusses how to use the <code>JFileChooser</code> API. A <code>JFileChooser</code> object only presents the GUI for choosing files. Your program is responsible for doing something with the chosen file, such as opening or saving it. Refer to 
<a class="TutorialLink" target="_top" href="http://docs.oracle.com/javase/tutorial/essential/io/index.html">Basic I/O</a> for information on how to read and write files.</p>
<p>The <code>JFileChooser</code> API makes it easy to bring up open and save dialogs. The type of look and feel determines what these standard dialogs look like and how they differ. In the Java look and feel, the save dialog looks the same as the open dialog, except for the title on the dialog's window and the text on the button that approves the operation. Here is a picture of a standard open dialog in the Java look and feel:</p>
<center><img src="./File Choosers_files/FileChooserOpenMetal.png" width="508" height="360" align="bottom" alt="A standard open dialog shown in the Java look and feel"></center><p>Here is a picture of an application called <code>FileChooserDemo</code> that brings up an open dialog and a save dialog.</p>
<center><img src="./File Choosers_files/FileChooserDemo2Metal.png" width="272" height="163" align="bottom" alt="A program that brings up an open or save dialog"></center><div class="note"><hr><strong>Try this:</strong>&nbsp;<ol>
<li>Compile and run the example, consult the <a href="http://docs.oracle.com/javase/tutorial/uiswing/examples/components/index.html#FileChooserDemo">example index</a>.</li>
<li>Click the Open a File button. Navigate around the file chooser, choose a file, and click the dialog's Open button.</li>
<li>Use the Save a File button to bring up a save dialog. Try to use all of the controls on the file chooser.</li>
<li>In the source file 
<a class="SourceLink" target="_blank" href="http://docs.oracle.com/javase/tutorial/uiswing/examples/components/FileChooserDemoProject/src/components/FileChooserDemo.java" onclick="showCode(&#39;../../displayCode.html&#39;, &#39;../examples/components/FileChooserDemoProject/src/components/FileChooserDemo.java&#39;); return false;"><code>FileChooserDemo.java</code></a>, change the file selection mode to directories-only mode. (Search for <code>DIRECTORIES_ONLY</code> and uncomment the line that contains it.) Then compile and run the example again. You will only be able to see and select directories, not ordinary files.</li>
</ol>
<hr></div>
<p>Bringing up a standard open dialog requires only two lines of code:</p>
<div class="codeblock"><pre>//Create a file chooser
final JFileChooser fc = new JFileChooser();
...
<em>//In response to a button click:</em>
int returnVal = fc.showOpenDialog(<em>aComponent</em>);
</pre></div>
<p>The argument to the <code>showOpenDialog</code> method specifies the parent component for the dialog. The parent component affects the position of the dialog and the frame that the dialog depends on. For example, the Java look and feel places the dialog directly over the parent component. If the parent component is in a frame, then the dialog is dependent on that frame. This dialog disappears when the frame is minimized and reappears when the frame is maximized.</p>
<p>By default, a file chooser that has not been shown before displays all files in the user's home directory. You can specify the file chooser's initial directory by using one of <code>JFileChooser</code>'s other constructors, or you can set the directory with the <code>setCurrentDirectory</code> method.</p>
<p>The call to <code>showOpenDialog</code> appears in the <code>actionPerformed</code> method of the Open a File button's action listener:</p>
<div class="codeblock"><pre>public void actionPerformed(ActionEvent e) {
    //Handle open button action.
    if (e.getSource() == openButton) {
        int returnVal = fc.showOpenDialog(FileChooserDemo.this);

        if (returnVal == JFileChooser.APPROVE_OPTION) {
            File file = fc.getSelectedFile();
            //This is where a real application would open the file.
            log.append("Opening: " + file.getName() + "." + newline);
        } else {
            log.append("Open command cancelled by user." + newline);
        }
   } ...
}
</pre></div>
<p>The <code>show<em>Xxx</em>Dialog</code> methods return an integer that indicates whether the user selected a file. Depending on how you use a file chooser, it is often sufficient to check whether the return value is <code>APPROVE_OPTION</code> and then not to change any other value. To get the chosen file (or directory, if you set up the file chooser to allow directory selections), call the <code>getSelectedFile</code> method on the file chooser. This method returns an instance of 
<a class="APILink" target="_blank" href="http://docs.oracle.com/javase/7/docs/api/java/io/File.html"><code>File</code></a>.</p>
<p>The example obtains the name of the file and uses it in the log message. You can call other methods on the <code>File</code> object, such as <code>getPath</code>, <code>isDirectory</code>, or <code>exists</code> to obtain information about the file. You can also call other methods such as <code>delete</code> and <code>rename</code> to change the file in some way. Of course, you might also want to open or save the file by using one of the reader or writer classes provided by the Java platform. See 
<a class="TutorialLink" target="_top" href="http://docs.oracle.com/javase/tutorial/essential/io/index.html">Basic I/O</a> for information about using readers and writers to read and write data to the file system.</p>
<p>The example program uses the same instance of the <code>JFileChooser</code> class to display a standard save dialog. This time the program calls <code>showSaveDialog</code>:</p>
<div class="codeblock"><pre>int returnVal = fc.showSaveDialog(FileChooserDemo.this);
</pre></div>
<p>By using the same file chooser instance to display its open and save dialogs, the program reaps the following benefits:</p>
<ul>
<li>The chooser remembers the current directory between uses, so the open and save versions automatically share the same current directory.</li>
<li>You have to customize only one file chooser, and the customizations apply to both the open and save versions.</li>
</ul>
<p>Finally, the example program has commented-out lines of code that let you change the file selection mode. For example, the following line of code makes the file chooser able to select only directories, and not files:</p>
<div class="codeblock"><pre>fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
</pre></div>
<p>Another possible selection mode is <code>FILES_AND_DIRECTORIES</code>. The default is <code>FILES_ONLY</code>. The following picture shows an open dialog with the file selection mode set to <code>DIRECTORIES_ONLY</code>. Note that, in the Java look and feel at least, only directories are visible — not files.</p>
<center><img src="./File Choosers_files/FileChooserDirectoriesMetal.png" width="508" height="360" align="bottom" alt="A file chooser in DIRECTORIES_ONLY mode"></center><p>If you want to create a file chooser for a task other than opening or saving, or if you want to customize the file chooser, keep reading. 
We will discuss the following topics:

</p><ul>
<li> <a href="http://docs.oracle.com/javase/tutorial/uiswing/components/filechooser.html#advancedexample">Another Example: FileChooserDemo2</a>
</li><li> <a href="http://docs.oracle.com/javase/tutorial/uiswing/components/filechooser.html#customtask">Using a File Chooser for a Custom Task</a>
</li><li> <a href="http://docs.oracle.com/javase/tutorial/uiswing/components/filechooser.html#filters">Filtering the List of Files</a>
</li><li> <a href="http://docs.oracle.com/javase/tutorial/uiswing/components/filechooser.html#fileview">Customizing the File View</a>
</li><li> <a href="http://docs.oracle.com/javase/tutorial/uiswing/components/filechooser.html#accessory">Providing an Accessory Component</a>
</li><li> <a href="http://docs.oracle.com/javase/tutorial/uiswing/components/filechooser.html#api">The File Chooser API</a>
</li><li> <a href="http://docs.oracle.com/javase/tutorial/uiswing/components/filechooser.html#eg">Examples that Use File Choosers</a>
</li></ul>
 <a name="advancedexample" id="advancedexample"></a><p></p>
<h2><a name="advancedexample__1" id="advancedexample__1">Another Example: FileChooserDemo2</a></h2>
<p>Let us look at <a href="http://docs.oracle.com/javase/tutorial/uiswing/examples/components/index.html#FileChooserDemo2"><code>FileChooserDemo2</code></a> example, a modified version of the previous demo program that uses more of the <code>JFileChooser</code> API. This example uses a file chooser that has been customized in several ways. Like the original example, the user invokes a file chooser with the push of a button. Here is a picture of the file chooser:</p>
<center><img src="./File Choosers_files/FileChooserDemo3Metal.png" width="536" height="360" align="bottom" alt="A file chooser with various customizations"></center><p>As the figure shows, this file chooser has been customized for a special task (Attach), provides a user-choosable file filter (Just Images), uses a special file view for image files, and has an accessory component that displays a thumbnail sketch of the currently selected image file.</p>
<p>The remainder of this section shows you the code that creates and customizes this file chooser. See the <a href="http://docs.oracle.com/javase/tutorial/uiswing/examples/components/index.html#FileChooserDemo2">example index</a> for links to all the files required by this example.</p>
<h2><a name="customtask" id="customtask">Using a File Chooser for a Custom Task</a></h2>
<p>As you have seen, the <code>JFileChooser</code> class provides the <code>showOpenDialog</code> method for displaying an open dialog and the <code>showSaveDialog</code> method for displaying a save dialog.</p>
<p>The class has another method, <code>showDialog</code>, for displaying a file chooser for a custom task in a dialog. In the Java look and feel, the only difference between this dialog and the other file chooser dialogs is the title on the dialog window and the label on the approve button. Here is the code from <code>FileChooserDemo2</code> that brings up the file chooser dialog for the Attach task:</p>
<div class="codeblock"><pre>JFileChooser fc = new JFileChooser();
int returnVal = fc.showDialog(FileChooserDemo2.this, "Attach");
</pre></div>
<p>The first argument to the <code>showDialog</code> method is the parent component for the dialog. The second argument is a <code>String</code> object that provides both the title for the dialog window and the label for the approve button.</p>
<p>Once again, the file chooser doesn't do anything with the selected file. The program is responsible for implementing the custom task for which the file chooser was created.</p>
<h2><a name="filters" id="filters">Filtering the List of Files</a></h2>
<p>By default, a file chooser displays all of the files and directories that it detects, except for hidden files. A program can apply one or more <em>file filters</em> to a file chooser so that the chooser shows only some files. The file chooser calls the filter's <code>accept</code> method for each file to determine whether it should be displayed. A file filter accepts or rejects a file based on criteria such as file type, size, ownership, and so on. Filters affect the list of files displayed by the file chooser. The user can enter the name of any file even if it is not displayed.</p>
<p><code>JFileChooser</code> supports three different kinds of filtering. The filters are checked in the order listed here. For example, an application-controlled filter sees only those files accepted by the built-in filtering.</p>
<dl>
<dt><strong>Built-in filtering</strong></dt>
<dd>Filtering is set up through specific method calls on a file chooser. Currently the only built-in filter available is for hidden files, such as those whose names begin with period (.) on UNIX systems. By default, hidden files are not shown. Call <code>setFileHidingEnabled(false)</code> to show hidden files.</dd>
<dt><strong>Application-controlled filtering</strong></dt>
<dd>The application determines which files are shown. Create a custom subclass of 
<a class="APILink" target="_blank" href="http://docs.oracle.com/javase/7/docs/api/javax/swing/filechooser/FileFilter.html"><code>FileFilter</code></a>, instantiate it, and use the instance as an argument to the <code>setFileFilter</code> method. The installed filter is displayed on the list of user-choosable filters. The file chooser shows only those files that the filter accepts.</dd>
<dt><strong>User-choosable filtering</strong></dt>
<dd>The file chooser GUI provides a list of filters that the user can choose from. When the user chooses a filter, the file chooser shows only those files accepted by that filter. <code>FileChooserDemo2</code> adds a custom file filter to the list of user-choosable filters:
<div class="codeblock"><pre>fc.addChoosableFileFilter(new ImageFilter());
</pre></div>
By default, the list of user-choosable filters includes the Accept All filter, which enables the user to see all non-hidden files. This example uses the following code to disable the Accept All filter:
<div class="codeblock"><pre>fc.setAcceptAllFileFilterUsed(false);
</pre></div>
Our custom file filter is implemented in 
<a class="SourceLink" target="_blank" href="http://docs.oracle.com/javase/tutorial/uiswing/examples/components/FileChooserDemo2Project/src/components/ImageFilter.java" onclick="showCode(&#39;../../displayCode.html&#39;, &#39;../examples/components/FileChooserDemo2Project/src/components/ImageFilter.java&#39;); return false;"><code>ImageFilter.java</code></a> and is a subclass of <code>FileFilter</code>. The <code>ImageFilter</code> class implements the <code>getDescription</code> method to return "Just Images" — a string to put in the list of user-choosable filters. <code>ImageFilter</code> also implements the <code>accept</code> method so that it accepts all directories and any file that has a <code>.png</code>, <code>.jpg</code>, <code>.jpeg</code>, <code>.gif</code>, <code>.tif</code>, or <code>.tiff</code> filename extension.
<div class="codeblock"><pre>public boolean accept(File f) {
    <strong>if (f.isDirectory()) {
        return true;
    }</strong>

    String extension = Utils.getExtension(f);
    if (extension != null) {
        if (extension.equals(Utils.tiff) ||
            extension.equals(Utils.tif) ||
            extension.equals(Utils.gif) ||
            extension.equals(Utils.jpeg) ||
            extension.equals(Utils.jpg) ||
            extension.equals(Utils.png)) {
                return true;
        } else {
            return false;
        }
    }

    return false;
}
</pre></div>
By accepting all directories, this filter allows the user to navigate around the file system. If the bold lines were omitted from this method, the user would be limited to the directory with which the chooser was initialized.
<p>The preceding code sample uses the <code>getExtension</code> method and several string constants from 
<a class="SourceLink" target="_blank" href="http://docs.oracle.com/javase/tutorial/uiswing/examples/components/FileChooserDemo2Project/src/components/Utils.java" onclick="showCode(&#39;../../displayCode.html&#39;, &#39;../examples/components/FileChooserDemo2Project/src/components/Utils.java&#39;); return false;"><code>Utils.java</code></a>, shown here:</p>
<div class="codeblock"><pre>public class Utils {

    public final static String jpeg = "jpeg";
    public final static String jpg = "jpg";
    public final static String gif = "gif";
    public final static String tiff = "tiff";
    public final static String tif = "tif";
    public final static String png = "png";

    /*
     * Get the extension of a file.
     */  
    public static String getExtension(File f) {
        String ext = null;
        String s = f.getName();
        int i = s.lastIndexOf('.');

        if (i &gt; 0 &amp;&amp;  i &lt; s.length() - 1) {
            ext = s.substring(i+1).toLowerCase();
        }
        return ext;
    }
}
</pre></div>
</dd>
</dl>
<h2><a name="fileview" id="fileview">Customizing the File View</a></h2>
<p>In the Java look and feel, the chooser's list shows each file's name and displays a small icon that represents whether the file is a true file or a directory. You can customize this <em>file view</em> by creating a custom subclass of 
<a class="APILink" target="_blank" href="http://docs.oracle.com/javase/7/docs/api/javax/swing/filechooser/FileView.html"><code>FileView</code></a> and using an instance of the class as an argument to the <code>setFileView</code> method. The example uses an instance of a custom class, implemented in 
<a class="SourceLink" target="_blank" href="http://docs.oracle.com/javase/tutorial/uiswing/examples/components/FileChooserDemo2Project/src/components/ImageFileView.java" onclick="showCode(&#39;../../displayCode.html&#39;, &#39;../examples/components/FileChooserDemo2Project/src/components/ImageFileView.java&#39;); return false;"><code>ImageFileView.java</code></a>, as the file chooser's file view.</p>
<div class="codeblock"><pre>fc.setFileView(new ImageFileView());
</pre></div>
<p>The <code>ImageFileView</code> class shows a different icon for each type of image accepted by the image filter described previously.</p>
<p>The <code>ImageFileView</code> class overrides the five abstract methods defined in the <code>FileView</code> as follows.</p>
<dl>
<dt><strong><code>String getTypeDescription(File f)</code></strong></dt>
<dd>Returns a description of the file type. Here is <code>ImageFileView</code>'s implementation of this method:
<div class="codeblock"><pre>public String getTypeDescription(File f) {
    String extension = Utils.getExtension(f);
    String type = null;

    if (extension != null) {
        if (extension.equals(Utils.jpeg) ||
            extension.equals(Utils.jpg)) {
            type = "JPEG Image";
        } else if (extension.equals(Utils.gif)){
            type = "GIF Image";
        } else if (extension.equals(Utils.tiff) ||
                   extension.equals(Utils.tif)) {
            type = "TIFF Image";
        } else if (extension.equals(Utils.png)){
            type = "PNG Image";
        }
    }
    return type;
}
</pre></div>
</dd>
<dt><strong><code>Icon getIcon(File f)</code></strong></dt>
<dd>Returns an icon representing the file or its type. Here is <code>ImageFileView</code>'s implementation of this method:
<div class="codeblock"><pre>public Icon getIcon(File f) {
    String extension = Utils.getExtension(f);
    Icon icon = null;

    if (extension != null) {
        if (extension.equals(Utils.jpeg) ||
            extension.equals(Utils.jpg)) {
            icon = jpgIcon;
        } else if (extension.equals(Utils.gif)) {
            icon = gifIcon;
        } else if (extension.equals(Utils.tiff) ||
                   extension.equals(Utils.tif)) {
            icon = tiffIcon;
        } else if (extension.equals(Utils.png)) {
            icon = pngIcon;
        }
    }
    return icon;
}
</pre></div>
</dd>
<dt><strong><code>String getName(File f)</code></strong></dt>
<dd>Returns the name of the file. Most implementations of this method should return <code>null</code> to indicate that the look and feel should figure it out. Another common implementation returns <code>f.getName()</code>.</dd>
<dt><strong><code>String getDescription(File f)</code></strong></dt>
<dd>Returns a description of the file. The intent is to describe individual files more specifically. A common implementation of this method returns <code>null</code> to indicate that the look and feel should figure it out.</dd>
<dt><strong><code>Boolean isTraversable(File f)</code></strong></dt>
<dd>Returns whether a directory is traversable. Most implementations of this method should return <code>null</code> to indicate that the look and feel should figure it out. Some applications might want to prevent users from descending into a certain type of directory because it represents a compound document. The <code>isTraversable</code> method should never return <code>true</code> for a non-directory.</dd>
</dl>
<h2><a name="accessory" id="accessory">Providing an Accessory Component</a></h2>
<p>The customized file chooser in <code>FileChooserDemo2</code> has an accessory component. If the currently selected item is a PNG, JPEG, TIFF, or GIF image, the accessory component displays a thumbnail sketch of the image. Otherwise, the accessory component is empty. Aside from a previewer, probably the most common use for the accessory component is a panel with more controls on it such as checkboxes that toggle between features.</p>
<p>The example calls the <code>setAccessory</code> method to establish an instance of the <code>ImagePreview</code> class, implemented in 
<a class="SourceLink" target="_blank" href="http://docs.oracle.com/javase/tutorial/uiswing/examples/components/FileChooserDemo2Project/src/components/ImagePreview.java" onclick="showCode(&#39;../../displayCode.html&#39;, &#39;../examples/components/FileChooserDemo2Project/src/components/ImagePreview.java&#39;); return false;"><code>ImagePreview.java</code></a>, as the chooser's accessory component:</p>
<div class="codeblock"><pre>fc.setAccessory(new ImagePreview(fc));
</pre></div>
<p>Any object that inherits from the <code>JComponent</code> class can be an accessory component. The component should have a preferred size that looks good in the file chooser.</p>
<p>The file chooser fires a property change event when the user selects an item in the list. A program with an accessory component must register to receive these events to update the accessory component whenever the selection changes. In the example, the <code>ImagePreview</code> object itself registers for these events. This keeps all the code related to the accessory component together in one class.</p>
<p>Here is the example's implementation of the <code>propertyChange</code> method, which is the method called when a property change event is fired:</p>
<div class="codeblock"><pre>//<em>where member variables are declared</em>
File file = null;
...
public void propertyChange(PropertyChangeEvent e) {
    boolean update = false;
    String prop = e.getPropertyName();

    //If the directory changed, don't show an image.
    if (JFileChooser.DIRECTORY_CHANGED_PROPERTY.equals(prop)) {
        file = null;
        update = true;

    //If a file became selected, find out which one.
    } else if (JFileChooser.SELECTED_FILE_CHANGED_PROPERTY.equals(prop)) {
        file = (File) e.getNewValue();
        update = true;
    }

    //Update the preview accordingly.
    if (update) {
        thumbnail = null;
        if (isShowing()) {
            loadImage();
            repaint();
        }
    }
}
</pre></div>
<p>If <code>SELECTED_FILE_CHANGED_PROPERTY</code> is the property that changed, this method obtains a <code>File</code> object from the file chooser. The <code>loadImage</code> and <code>repaint</code> methods use the <code>File</code> object to load the image and repaint the accessory component.</p>
<h2><a name="api" id="api">The File Chooser API</a></h2>
<p>The API for using file choosers falls into these categories:</p>
<ul>
<li><a href="http://docs.oracle.com/javase/tutorial/uiswing/components/filechooser.html#show">Creating and Showing the File Chooser</a></li>
<li><a href="http://docs.oracle.com/javase/tutorial/uiswing/components/filechooser.html#selection">Selecting Files and Directories</a></li>
<li><a href="http://docs.oracle.com/javase/tutorial/uiswing/components/filechooser.html#navigation">Navigating the File Chooser's List</a></li>
<li><a href="http://docs.oracle.com/javase/tutorial/uiswing/components/filechooser.html#customization">Customizing the File Chooser</a></li>
</ul>
<table border="1" summary="Creating and Showing the File Chooser">
<caption><a style="font-weight: bold" name="show" id="show">Creating and Showing the File Chooser</a></caption>
<tbody><tr>
<th id="h1" align="left">Method or Constructor</th>
<th id="h2" align="left">Purpose</th>
</tr>
<tr>
<td headers="h1">
<a class="APILink" target="_blank" href="http://docs.oracle.com/javase/7/docs/api/javax/swing/JFileChooser.html#JFileChooser()">JFileChooser()</a><br>
<a class="APILink" target="_blank" href="http://docs.oracle.com/javase/7/docs/api/javax/swing/JFileChooser.html#JFileChooser(java.io.File)">JFileChooser(File)</a><br>
<a class="APILink" target="_blank" href="http://docs.oracle.com/javase/7/docs/api/javax/swing/JFileChooser.html#JFileChooser(java.lang.String)">JFileChooser(String)</a></td>
<td headers="h2">Creates a file chooser instance. The <code>File</code> and <code>String</code> arguments, when present, provide the initial directory.</td>
</tr>
<tr>
<td headers="h1">
<a class="APILink" target="_blank" href="http://docs.oracle.com/javase/7/docs/api/javax/swing/JFileChooser.html#showOpenDialog(java.awt.Component)">int showOpenDialog(Component)</a><br>
<a class="APILink" target="_blank" href="http://docs.oracle.com/javase/7/docs/api/javax/swing/JFileChooser.html#showSaveDialog(java.awt.Component)">int showSaveDialog(Component)</a><br>
<a class="APILink" target="_blank" href="http://docs.oracle.com/javase/7/docs/api/javax/swing/JFileChooser.html#showDialog(java.awt.Component, java.lang.String)">int showDialog(Component, String)</a></td>
<td headers="h2">Shows a modal dialog containing the file chooser. These methods return <code>APPROVE_OPTION</code> if the user approved the operation and <code>CANCEL_OPTION</code> if the user cancelled it. Another possible return value is <code>ERROR_OPTION</code>, which means an unanticipated error occurred.</td>
</tr>
</tbody></table>
<table border="1" summary="Selecting Files and Directories">
<caption><a style="font-weight: bold" name="selection" id="selection">Selecting Files and Directories</a></caption>
<tbody><tr>
<th id="h101" align="left">Method</th>
<th id="h102" align="left">Purpose</th>
</tr>
<tr>
<td headers="h101">
<a class="APILink" target="_blank" href="http://docs.oracle.com/javase/7/docs/api/javax/swing/JFileChooser.html#setSelectedFile(java.io.File)">void setSelectedFile(File)</a><br>
<a class="APILink" target="_blank" href="http://docs.oracle.com/javase/7/docs/api/javax/swing/JFileChooser.html#getSelectedFile()">File getSelectedFile()</a></td>
<td headers="h102">Sets or obtains the currently selected file or (if directory selection has been enabled) directory.</td>
</tr>
<tr>
<td headers="h101">
<a class="APILink" target="_blank" href="http://docs.oracle.com/javase/7/docs/api/javax/swing/JFileChooser.html#setSelectedFiles(java.io.File[])">void setSelectedFiles(File[])</a><br>
<a class="APILink" target="_blank" href="http://docs.oracle.com/javase/7/docs/api/javax/swing/JFileChooser.html#getSelectedFiles()">File[] getSelectedFiles()</a></td>
<td headers="h102">Sets or obtains the currently selected files if the file chooser is set to allow multiple selection.</td>
</tr>
<tr>
<td headers="h101">
<a class="APILink" target="_blank" href="http://docs.oracle.com/javase/7/docs/api/javax/swing/JFileChooser.html#setFileSelectionMode(int)">void setFileSelectionMode(int)</a><br>
<a class="APILink" target="_blank" href="http://docs.oracle.com/javase/7/docs/api/javax/swing/JFileChooser.html#getFileSelectionMode()">void getFileSelectionMode()</a><br>
<a class="APILink" target="_blank" href="http://docs.oracle.com/javase/7/docs/api/javax/swing/JFileChooser.html#isDirectorySelectionEnabled()">boolean isDirectorySelectionEnabled()</a><br>
<a class="APILink" target="_blank" href="http://docs.oracle.com/javase/7/docs/api/javax/swing/JFileChooser.html#isFileSelectionEnabled()">boolean isFileSelectionEnabled()</a></td>
<td headers="h102">Sets or obtains the file selection mode. Acceptable values are <code>FILES_ONLY</code> (the default), <code>DIRECTORIES_ONLY</code>, and <code>FILES_AND_DIRECTORIES</code>.<br>
Interprets whether directories or files are selectable according to the current selection mode.</td>
</tr>
<tr>
<td headers="h101">
<a class="APILink" target="_blank" href="http://docs.oracle.com/javase/7/docs/api/javax/swing/JFileChooser.html#setMultiSelectionEnabled(boolean)">void setMultiSelectionEnabled(boolean)</a><br>
<a class="APILink" target="_blank" href="http://docs.oracle.com/javase/7/docs/api/javax/swing/JFileChooser.html#isMultiSelectionEnabled()">boolean isMultiSelectionEnabled()</a></td>
<td headers="h102">Sets or interprets whether multiple files can be selected at once. By default, a user can choose only one file.</td>
</tr>
<tr>
<td headers="h101">
<a class="APILink" target="_blank" href="http://docs.oracle.com/javase/7/docs/api/javax/swing/JFileChooser.html#setAcceptAllFileFilterUsed(boolean)">void setAcceptAllFileFilterUsed(boolean)</a><br>
<a class="APILink" target="_blank" href="http://docs.oracle.com/javase/7/docs/api/javax/swing/JFileChooser.html#isAcceptAllFileFilterUsed()">boolean isAcceptAllFileFilterUsed()</a></td>
<td headers="h102">Sets or obtains whether the <code>AcceptAll</code> file filter is used as an allowable choice in the choosable filter list; the default value is <code>true</code>.</td>
</tr>
<tr>
<td headers="h101">
<a class="APILink" target="_blank" href="http://docs.oracle.com/javase/7/docs/api/javax/swing/JFileChooser.html#createDialog(java.awt.Component)">Dialog createDialog(Component)</a></td>
<td headers="h102">Given a parent component, creates and returns a new dialog that contains this file chooser, is dependent on the parent's frame, and is centered over the parent.</td>
</tr>
</tbody></table>
<table border="1" summary="Navigating the File Chooser&#39;s List">
<caption><a style="font-weight: bold" name="navigation" id="navigation">Navigating the File Chooser's List</a></caption>
<tbody><tr>
<th id="h201" align="left">Method</th>
<th id="h202" align="left">Purpose</th>
</tr>
<tr>
<td headers="h201">
<a class="APILink" target="_blank" href="http://docs.oracle.com/javase/7/docs/api/javax/swing/JFileChooser.html#ensureFileIsVisible(java.io.File)">void ensureFileIsVisible(File)</a></td>
<td headers="h202">Scrolls the file chooser's list such that the indicated file is visible.</td>
</tr>
<tr>
<td headers="h201">
<a class="APILink" target="_blank" href="http://docs.oracle.com/javase/7/docs/api/javax/swing/JFileChooser.html#setCurrentDirectory(java.io.File)">void setCurrentDirectory(File)</a><br>
<a class="APILink" target="_blank" href="http://docs.oracle.com/javase/7/docs/api/javax/swing/JFileChooser.html#getCurrentDirectory()">File getCurrentDirectory()</a></td>
<td headers="h202">Sets or obtains the directory whose files are displayed in the file chooser's list.</td>
</tr>
<tr>
<td headers="h201">
<a class="APILink" target="_blank" href="http://docs.oracle.com/javase/7/docs/api/javax/swing/JFileChooser.html#changeToParentDirectory()">void changeToParentDirectory()</a></td>
<td headers="h202">Changes the list to display the current directory's parent.</td>
</tr>
<tr>
<td headers="h201">
<a class="APILink" target="_blank" href="http://docs.oracle.com/javase/7/docs/api/javax/swing/JFileChooser.html#rescanCurrentDirectory()">void rescanCurrentDirectory()</a></td>
<td headers="h202">Checks the file system and updates the chooser's list.</td>
</tr>
<tr>
<td headers="h201">
<a class="APILink" target="_blank" href="http://docs.oracle.com/javase/7/docs/api/javax/swing/JFileChooser.html#setDragEnabled(boolean)">void setDragEnabled(boolean)</a><br>
<a class="APILink" target="_blank" href="http://docs.oracle.com/javase/7/docs/api/javax/swing/JFileChooser.html#getDragEnabled()">boolean getDragEnabled()</a></td>
<td headers="h202">Sets or obtains the property that determines whether automatic drag handling is enabled. See 
<a class="TutorialLink" target="_top" href="http://docs.oracle.com/javase/tutorial/uiswing/dnd/index.html">Drag and Drop and Data Transfer</a> for more details.</td>
</tr>
</tbody></table>
<table border="1" summary="Customizing the File Chooser">
<caption><a style="font-weight: bold" name="customization" id="customization">Customizing the File Chooser</a></caption>
<tbody><tr>
<th id="h301" align="left">Method</th>
<th id="h302" align="left">Purpose</th>
</tr>
<tr>
<td headers="h301">
<a class="APILink" target="_blank" href="http://docs.oracle.com/javase/7/docs/api/javax/swing/JFileChooser.html#setAccessory(javax.swing.JComponent)">void setAccessory(javax.swing.JComponent)</a><br>
<a class="APILink" target="_blank" href="http://docs.oracle.com/javase/7/docs/api/javax/swing/JFileChooser.html#getAccessory()">JComponent getAccessory()</a></td>
<td headers="h302">Sets or obtains the file chooser's accessory component.</td>
</tr>
<tr>
<td headers="h301">
<a class="APILink" target="_blank" href="http://docs.oracle.com/javase/7/docs/api/javax/swing/JFileChooser.html#setFileFilter(javax.swing.filechooser.FileFilter)">void setFileFilter(FileFilter)</a><br>
<a class="APILink" target="_blank" href="http://docs.oracle.com/javase/7/docs/api/javax/swing/JFileChooser.html#getFileFilter()">FileFilter getFileFilter()</a></td>
<td headers="h302">Sets or obtains the file chooser's primary file filter.</td>
</tr>
<tr>
<td headers="h301">
<a class="APILink" target="_blank" href="http://docs.oracle.com/javase/7/docs/api/javax/swing/JFileChooser.html#setFileView(javax.swing.filechooser.FileView)">void setFileView(FileView)</a><br>
<a class="APILink" target="_blank" href="http://docs.oracle.com/javase/7/docs/api/javax/swing/JFileChooser.html#getFileView()">FileView getFileView()</a></td>
<td headers="h302">Sets or obtains the chooser's file view.</td>
</tr>
<tr>
<td headers="h301">
<a class="APILink" target="_blank" href="http://docs.oracle.com/javase/7/docs/api/javax/swing/JFileChooser.html#getChoosableFileFilters()">FileFilter[] getChoosableFileFilters()</a><br>
<a class="APILink" target="_blank" href="http://docs.oracle.com/javase/7/docs/api/javax/swing/JFileChooser.html#addChoosableFileFilter(javax.swing.filechooser.FileFilter)">void addChoosableFileFilter(FileFilter)</a><br>
<a class="APILink" target="_blank" href="http://docs.oracle.com/javase/7/docs/api/javax/swing/JFileChooser.html#removeChoosableFileFilter(javax.swing.filechooser.FileFilter)">boolean removeChoosableFileFilter(FileFilter)</a><br>
<a class="APILink" target="_blank" href="http://docs.oracle.com/javase/7/docs/api/javax/swing/JFileChooser.html#resetChoosableFileFilters()">void resetChoosableFileFilters()</a><br>
<a class="APILink" target="_blank" href="http://docs.oracle.com/javase/7/docs/api/javax/swing/JFileChooser.html#getAcceptAllFileFilter()">FileFilter getAcceptAllFileFilter()</a></td>
<td headers="h302">Sets, obtains, or modifies the list of user-choosable file filters.</td>
</tr>
<tr>
<td headers="h301">
<a class="APILink" target="_blank" href="http://docs.oracle.com/javase/7/docs/api/javax/swing/JFileChooser.html#setFileHidingEnabled(boolean)">void setFileHidingEnabled(boolean)</a><br>
<a class="APILink" target="_blank" href="http://docs.oracle.com/javase/7/docs/api/javax/swing/JFileChooser.html#isFileHidingEnabled()">boolean isFileHidingEnabled()</a></td>
<td headers="h302">Sets or obtains whether hidden files are displayed.</td>
</tr>
<tr>
<td headers="h301">
<a class="APILink" target="_blank" href="http://docs.oracle.com/javase/7/docs/api/javax/swing/JFileChooser.html#setControlButtonsAreShown(boolean)">void setControlButtonsAreShown(boolean)</a><br>
<a class="APILink" target="_blank" href="http://docs.oracle.com/javase/7/docs/api/javax/swing/JFileChooser.html#getControlButtonsAreShown()">boolean getControlButtonsAreShown()</a></td>
<td headers="h302">Sets or obtains the property that indicates whether the Approve and Cancel buttons are shown in the file chooser. This property is true by default.</td>
</tr>
</tbody></table>
<h2><a name="eg" id="eg">Examples That Use File Choosers</a></h2>
<p>This table shows the examples that use file choosers and points to where those examples are described.</p>
<table summary="Examples That Use File Choosers">
<tbody><tr>
<th id="h401" align="left">Example</th>
<th id="h402" align="left">Where Described</th>
<th id="h403" align="left">Notes</th>
</tr>
<tr>
<td headers="h401"><a href="http://docs.oracle.com/javase/tutorial/uiswing/examples/components/index.html#FileChooserDemo"><code>FileChooserDemo</code></a></td>
<td headers="h402">This section</td>
<td headers="h403">Displays an open dialog and a save dialog.</td>
</tr>
<tr>
<td headers="h401"><a href="http://docs.oracle.com/javase/tutorial/uiswing/examples/components/index.html#FileChooserDemo2"><code>FileChooserDemo2</code></a></td>
<td headers="h402">This section</td>
<td headers="h403">Uses a file chooser with custom filtering, a custom file view, and an accessory component.</td>
</tr>
<tr>
<td headers="h401"><a href="http://docs.oracle.com/javase/tutorial/uiswing/examples/components/index.html#JWSFileChooserDemo"><code>JWSFileChooserDemo</code></a></td>
<td headers="h402">This section</td>
<td headers="h403">Uses the JNLP API to open and save files.</td>
</tr>
</tbody></table>


        </div>
        <div class="NavBit">
            <a target="_top" href="http://docs.oracle.com/javase/tutorial/uiswing/components/editorpane.html">« Previous</a>
            •
            <a target="_top" href="http://docs.oracle.com/javase/tutorial/uiswing/TOC.html">Trail</a>
            •
            <a target="_top" href="http://docs.oracle.com/javase/tutorial/uiswing/components/formattedtextfield.html">Next »</a>
        </div>
    </div>
    <div id="Footer2">
<hr>
<div id="TagNotes">
    <p class="footertext">Problems with the examples? Try <a target="_blank" href="http://docs.oracle.com/javase/tutorial/information/run-examples.html">Compiling and Running
        the Examples: FAQs</a>.
    <br>
    Complaints? Compliments? Suggestions? <a target="_blank" href="http://docs.oracle.com/javase/feedback.html">Give
    us your feedback</a>.
    </p>
</div> 

<div id="Footer">
<p class="footertext"><a name="license_info">Your use of this</a> page and all the material on pages under "The Java Tutorials" banner 
 is subject to these <a href="http://docs.oracle.com/javase/tutorial/information/cpyr.html">legal notices</a>.
</p>
<table border="0" cellspacing="0" cellpadding="5" summary="">
    <tbody><tr>
         <td headers="h501" width="20%">
	 <table width="100%" border="0" cellspacing="0" cellpadding="5">
            <tbody><tr>
              <td headers="h501" align="center"><img id="duke" src="./File Choosers_files/DukeWave.gif" width="55" height="55" alt="duke image"></td>
              <td headers="h502" align="left" valign="middle"><img id="oracle" src="./File Choosers_files/logo_oracle_footer.gif" width="100" height="29" alt="Oracle logo"></td>
           </tr>
          </tbody></table>
          </td>

          <td width="55%" valign="middle" align="center">
		<p class="footertext"><a href="http://www.oracle.com/us/corporate/index.html">About Oracle</a> | <a href="http://www.oracle.com/technology/index.html">Oracle Technology Network</a> | <a href="https://www.samplecode.oracle.com/servlets/CompulsoryClickThrough?type=TermsOfService">Terms of Service</a></p> 
	 </td>
          <td width="25%" valign="middle" align="right">
      		<p class="footertext">Copyright © 1995, 2011 Oracle and/or its affiliates. All rights reserved.</p>
	 </td>
     </tr>     
</tbody></table>
</div>
    </div>
    <div class="PrintHeaders">
        <b>Previous page:</b> How to Use Editor Panes and Text Panes
        <br><b>Next page:</b> How to Use Formatted Text Fields
    </div>
<!-- Start SiteCatalyst code   -->
<script type="text/javascript" language="JavaScript" src="./File Choosers_files/s_code_download.js"></script>
<script type="text/javascript" language="JavaScript" src="./File Choosers_files/s_code.js"></script>
<noscript>
A browser with JavaScript enabled is required for this page to operate properly.
</noscript>

<!-- ********** DO NOT ALTER ANYTHING BELOW THIS LINE ! *********** -->
<!--  Below code will send the info to Omniture server -->
<script type="text/javascript" language="javascript">var s_code=s.t();if(s_code)document.write(s_code)</script>

<!-- End SiteCatalyst code -->

 
</body></html>